using global::GoGameClient.script.global;
using Godot;
using GoGameClient.script.node.letter;

namespace GoGameClient.script.node.world;

public partial class UiControl : CanvasLayer
{
    private Control _instantiate;

    private Vector2I _vector2I = new(0, 0);
    private BloodControl _bloodControl;

    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        InitLetter();
        InitBlood();
    }

    private void InitLetter()
    {
        _instantiate = (LetterByCharacterGui)GD.Load<PackedScene>("res://scene/LetterByCharacter.tscn").Instantiate();
        _instantiate.GetNode<TextureButton>("SwitchButton").Pressed += LetterButtonPressed;
        AddChild(_instantiate);
    }

    private void InitBlood()
    {
        _bloodControl = GetNode<BloodControl>("BloodControl");
        var roleEntity = UserControl.GetInstance().GetRole();
        _bloodControl.SetMaxHp(roleEntity.MaxHp);
        _bloodControl.SetBlood(roleEntity.Blood);
        _bloodControl.SetCurrentHp(roleEntity.Hp);

        _bloodControl.SetMaxMp(roleEntity.MaxMp);
        _bloodControl.SetCurrentMp(roleEntity.Mp);

        _bloodControl.SetMaxExp(roleEntity.MaxExp);
        _bloodControl.SetExp(roleEntity.Exp);

        _bloodControl.SetAnger(roleEntity.Anger);
        _bloodControl.RefreshRole();
        _bloodControl.SetAvator(roleEntity.GetAvator());
    }


    private void MoveSignal(Vector2I vector)
    {
        switch (_instantiate)
        {
            case LetterByCharacterGui gui when vector == new Vector2I(0, 0):
                gui.SetMove(false);
                return;
            case LetterByCharacterGui gui:
                gui.SetMove(true);
                gui.SetPosition(_vector2I = vector);
                break;
            case LetterGui when vector == new Vector2I(0, 0):
                return;
            case LetterGui letterGui:
                letterGui.SetPosition(_vector2I = vector);
                break;
        }
    }

    private void LetterButtonPressed()
    {
        GD.Print("LetterButtonPressed");
        RemoveChild(_instantiate);
        _instantiate.QueueFree();
        _instantiate = (Control)GD.Load<PackedScene>("res://scene/Letter.tscn").Instantiate();
        AddChild(_instantiate);
        var textureButton = _instantiate.GetNode<TextureButton>("SwitchButton");
        textureButton.Pressed += LetterByCharacterButtonPressed;
        textureButton.FlipV = true;
        ((LetterGui)_instantiate).SetPosition(_vector2I);
    }

    private void LetterByCharacterButtonPressed()
    {
        RemoveChild(_instantiate);
        _instantiate.QueueFree();
        _instantiate = (Control)GD.Load<PackedScene>("res://scene/LetterByCharacter.tscn").Instantiate();
        AddChild(_instantiate);
        var textureButton = _instantiate.GetNode<TextureButton>("SwitchButton");
        textureButton.Pressed += LetterButtonPressed;
        textureButton.FlipV = false;
        ((LetterByCharacterGui)_instantiate).SetPosition(_vector2I);
    }
}